package com.cf.r1042_ak;

import java.io.*;
import java.util.ArrayList;

public class d {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(br);
        PrintWriter out = new PrintWriter(System.out);
        in.nextToken(); int T = (int) in.nval;
        while (T-- > 0) {
            in.nextToken(); int n = (int) in.nval;
            ArrayList<Integer>[] graph = new ArrayList[n + 1];
            for (int i = 1; i <= n; i++) graph[i] = new ArrayList<>();
            int[] degree = new int[n + 1];
            for (int i = 0; i < n - 1; i++) {
                in.nextToken(); int u = (int) in.nval;
                in.nextToken(); int v = (int) in.nval;
                graph[u].add(v);
                graph[v].add(u);
                degree[u]++;
                degree[v]++;
            }
            if (n == 2) {
                out.println(0);
                continue;
            }
            int total = 0;
            for (int i = 1; i <= n; i++) {
                if (degree[i] == 1) total++;
            }
            int max = 0;
            for (int u = 1; u <= n; u++) {
                int cnt = 0;
                for (int v : graph[u]) {
                    if (degree[v] == 1) cnt++;
                }
                max = Math.max(max, cnt);
            }

            out.println(total - max);
        }
        out.flush();
        out.close();
        br.close();
    }

}